import {
  InHistoryStats,
  InPoolStatus,
} from '@/api';
import echarts from '@/components/Echarts';
import echartsOptions from './echartsOptions';
import coinTr from './tr';

export default {
  props: {
    coin_data: {
      type: Object,
      default() {
        return { suanli: '', money: '', unit: '' };
      },
    },
  },
  components: {
    coinTr,
    echarts,
  },
  data() {
    return {
      shares: '',
      shares_unit: '',
      echartsOptions: JSON.parse(JSON.stringify(echartsOptions)),
    };
  },
  created() {
    this.getHistoryStats();
    this.getStats();
  },
  methods: {
    getHistoryStats() {
      InHistoryStats({ coin_type: this.coin_type }).then((res) => {
        if (res.data.status === 1) {
          const data = res.data.data.data;
          const xData = [];
          const yData = [];
          Object.keys(data).sort().slice(-7).forEach((date) => {
            yData.push(data[date]);
            xData.push(date);
          });
          this.echartsOptions.xAxis.data = xData;
          this.echartsOptions.series[0].data = yData;
          if (this.$refs.echarts) {
            this.$refs.echarts.setOption(this.echartsOptions);
          }
        }
      });
    },
    getStats() {
      InPoolStatus({ coin_type: this.coin_type }).then((res) => {
        if (res.data.status === 1) {
          const data = res.data.data;
          this.shares = data.shares ? data.shares : '0.00';
          this.shares_unit = data.shares_unit;
        }
      });
    },
  },
};
